package engine.gui;

import java.util.ArrayList;

public class Container extends Component {
	private ArrayList<Component> components;

	private boolean solid;		//if a panel is solid, it is drawn. I it is not solid, it will be invisible, but it will position correctly.
	
	public Container() {
		components = new ArrayList<Component>();
	}

	public ArrayList<Component> getComponents() {
		return components;
	}

	public void addComponent(Component component) {
		component.setWindowManager(getWindowManager());
		components.add(component);
	}

	public void removeComponent(Component component) {
		component.setWindowManager(null);
		components.remove(component);
	}

	@Override
	public void setWindowManager(WindowManager windowManager) {
		super.setWindowManager(windowManager);

		for (Component c : components) {
			c.setWindowManager(windowManager);
		}
	}

	@Override
	public void render(int x, int y, int width, int height) {
		for (Component c : components) {
			c.render(x + getPosX(), y + getPosY(), getWidth(), getHeight());
		}
	}

	@Override
	public void reset() {
		for (Component c : components) {
			c.reset();
		}
	}

	public boolean keyPressed(int key) {
		for (Component c : components) {
			if (c.keyPressed(key)) {
				return true;
			}
		}
		return false;
	}

	public boolean keyReleased(int key) {
		for (Component c : components) {
			if (c.keyReleased(key)) {
				return true;
			}
		}
		return false;
	}

	public boolean mousePressed(int posX, int posY, int button) {
		for (Component c : components) {
			if (c.mousePressed(posX - getPosX(), posY - getPosY(), button)) {
				return true;
			}
		}

		return isSolid() && posX > getPosX() && posX < getPosX() + getWidth()
				&& posY > getPosY() && posY < getPosY() + getHeight();
	}

	public boolean mouseReleased(int posX, int posY, int button) {
		for (Component c : components) {
			if (c.mouseReleased(posX - getPosX(), posY - getPosY(), button)) {
				return true;
			}
		}

		return isSolid() && posX > getPosX() && posX < getPosX() + getWidth()
				&& posY > getPosY() && posY < getPosY() + getHeight();
	}

	public boolean mouseMoved(int posX, int posY, int button) {
		if (posX > getPosX() && posX < getPosX() + getWidth()
				&& posY > getPosY() && posY < getPosY() + getHeight()) {
			for (Component c : components) {
				if (c.mouseMoved(posX - getPosX(), posY - getPosY(), button)) {
					return true;
				}
			}

			return isSolid();
		}
		return false;
	}

	public boolean mouseDragged(int posX, int posY, int button) {
		if (posX > getPosX() && posX < getPosX() + getWidth()
				&& posY > getPosY() && posY < getPosY() + getHeight()) {
			for (Component c : components) {
				if (c.mouseDragged(posX - getPosX(), posY - getPosY(), button)) {
					return true;
				}
			}

			return isSolid();
		}
		return false;
	}

	public boolean mouseWheelMoved(int movement) {
		for (Component c : components) {
			if (c.mouseWheelMoved(movement)) {
				return true;
			}
		}
		return false;
	}

	@Override
	public void activator() {
		if (components.size() > 0) {
			components.get(0).activator();
		}
	}
	
	public boolean isSolid() {
		return solid;
	}

	public void setSolid(boolean solid) {
		this.solid = solid;
	}
}
